import java.awt.*;
import java.util.Random;

public class Enemy {
    private static final int BASE_SPEED = 3;
    private static final int WIDTH = 50;
    private static final int HEIGHT = 50;

    private int x, y;
    private int speed;
    private Random random;
    private int health;
    private int points;
    private int type; // 0: 普通敌人, 1: 快速敌人, 2: 大型敌人

    public Enemy(int x, int y, int level) {
        this.x = x;
        this.y = y;
        random = new Random();

        // 随机选择敌人类型
        type = random.nextInt(3);

        // 根据类型设置属性
        switch (type) {
            case 0: // 普通敌人
                speed = BASE_SPEED + level / 2;
                health = 1;
                points = 10;
                break;
            case 1: // 快速敌人
                speed = BASE_SPEED + 2 + level / 2;
                health = 1;
                points = 15;
                break;
            case 2: // 大型敌人
                speed = BASE_SPEED - 1 + level / 4;
                health = 3;
                points = 25;
                break;
        }
    }

    public void update() {
        y += speed;

        // 随机横向移动
        if (random.nextInt(100) < 5) {
            x += (random.nextInt(3) - 1) * speed;
        }

        // 边界检查
        if (x < 0) x = 0;
        if (x > 800 - WIDTH) x = 800 - WIDTH;
    }

    public void draw(Graphics2D g2d) {
        switch (type) {
            case 0: // 普通敌人
                g2d.setColor(Color.RED);
                g2d.fillRect(x, y, WIDTH, HEIGHT);
                g2d.setColor(Color.BLACK);
                g2d.fillRect(x + 10, y + 10, 10, 10);
                g2d.fillRect(x + 30, y + 10, 10, 10);
                g2d.fillRect(x + 15, y + 30, 20, 5);
                break;
            case 1: // 快速敌人
                g2d.setColor(Color.MAGENTA);
                g2d.fillOval(x, y, WIDTH, HEIGHT);
                g2d.setColor(Color.BLACK);
                g2d.fillOval(x + 10, y + 10, 10, 10);
                g2d.fillOval(x + 30, y + 10, 10, 10);
                g2d.fillRect(x + 15, y + 30, 20, 5);
                break;
            case 2: // 大型敌人
                g2d.setColor(Color.ORANGE);
                g2d.fillRoundRect(x, y, WIDTH, HEIGHT, 10, 10);
                g2d.setColor(Color.BLACK);
                g2d.fillOval(x + 10, y + 10, 15, 15);
                g2d.fillOval(x + 25, y + 10, 15, 15);
                g2d.fillRect(x + 10, y + 35, 30, 5);
                break;
        }

        // 绘制生命值条
        if (health > 1) {
            g2d.setColor(Color.GREEN);
            g2d.fillRect(x, y - 5, WIDTH * health / 3, 3);
        }
    }

    public Rectangle getBounds() {
        return new Rectangle(x, y, WIDTH, HEIGHT);
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getPoints() {
        return points;
    }
}    